<template>
  <Dialog
    v-model="dialogVisible"
    title="添加客服"
    width="600"
    :before-close="clearFrom"
    class="add-customer-dialog"
  >
    <div class="form-container">
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="customer-form"
        :size="formSize"
        status-icon
        label-position="top"
      >
        <div class="form-grid">
          <el-form-item label="客服名称" prop="name" class="form-item">
            <el-input
              v-model="ruleForm.name"
              placeholder="请输入客服名称"
              class="form-input"
              clearable
            />
          </el-form-item>

          <el-form-item label="请选择分组" class="form-item">
            <el-select v-model="ruleForm.groupId" placeholder="请选择分组" class="form-select">
              <el-option
                v-for="item in leftSides"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="请选择欢迎语" class="form-item">
            <el-select v-model="ruleForm.welcomeId" placeholder="请选择欢迎语" class="form-select">
              <el-option
                v-for="item in simpleOption"
                :key="item.id"
                :label="item.configName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="请选择客服简介" class="form-item">
            <el-select
              v-model="ruleForm.introConfigId"
              placeholder="请选择客服简介"
              class="form-select"
            >
              <el-option
                v-for="item in introOption"
                :key="item.id"
                :label="item.configName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="请选择场景" class="form-item">
            <el-select v-model="ruleForm.sceneId" placeholder="请选择场景" class="form-select">
              <el-option
                v-for="item in sceneOption"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <el-form-item label="请选择所属员工" class="form-item">
            <el-select
              v-model="ruleForm.ownerUserId"
              placeholder="请选择所属员工"
              class="form-select"
              filterable
            >
              <el-option
                v-for="item in ownerUserOption"
                :key="item.id"
                :label="item.nickname"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </div>

        <el-form-item label="客服头像" class="form-item avatar-item">
          <div class="avatar-upload">
            <UploadImg v-model="ruleForm.avator" :limit="1" class="upload-component" />
          </div>
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" class="submit-btn" @click="submitForm(ruleFormRef)">
          <el-icon class="btn-icon"><Check /></el-icon>
          提交
        </el-button>
      </div>
    </template>
  </Dialog>
</template>

<script setup lang="ts">
import { Plus, Delete, View, Check } from '@element-plus/icons-vue'
import * as Assistant from '@/api/service/assistant/index'

const { leftSides, ownerUserOption, simpleOption, sceneOption, introOption } = defineProps({
  leftSides: Array,
  ownerUserOption: Array,
  simpleOption: Array,
  sceneOption: Array,
  introOption: Array
})
const dialogVisible = ref(false)
interface RuleForm {
  name: string
  groupId: string
  avator: string
  ownerUserId: string
  welcomeId: string
  sceneId: string
}
const emit = defineEmits(['sideRresh'])
const formSize = ref('default')
const ruleFormRef = ref(null)
const ruleForm = reactive<RuleForm>({
  name: '',
  groupId: '',
  avator: '',
  ownerUserId: '',
  welcomeId: '',
  sceneId: '',
  introConfigId: ''
})

const fromType = ref('')
const assistantId = ref('')

/*规则校验*/
const rules = reactive({
  name: [{ required: true, message: '请输入客服名称', trigger: 'blur' }],
  groupId: [
    {
      required: true,
      message: '请选择分组',
      trigger: 'change'
    }
  ],
  ownerUserId: [
    {
      required: true,
      message: '请选择所属员工',
      trigger: 'change'
    }
  ],
  welcomeId: [
    {
      required: true,
      message: '请选择欢迎语配置',
      trigger: 'change'
    }
  ]
})

/*提交*/
const submitForm = async (formEl) => {
  if (!formEl) return
  await formEl.validate(async (valid, fields) => {
    if (valid) {
      if (fromType.value == 'create') {
        const data = await Assistant.createAssistant(ruleForm)
        if (data) {
          if (data) ElMessage.success('添加成功')
          resetForm(ruleFormRef.value)
          dialogVisible.value = false
          emit('sideRresh')
        }
      } else {
        const data = await Assistant.updateAssistant({ ...ruleForm, id: assistantId.value })
        if (data) {
          if (data) ElMessage.success('更新成功')
          resetForm(ruleFormRef.value)
          dialogVisible.value = false
          emit('sideRresh')
        }
      }
    } else {
    }
  })
}
/*重置*/
const resetForm = (formEl) => {
  if (!formEl) return
  formEl.resetFields()
}
const clearFrom = () => {
  ruleForm.name = ''
  ruleForm.groupId = ''
  ruleForm.avator = ''
  ruleForm.ownerUserId = ''
  ruleForm.welcomeId = ''
  ruleForm.sceneId = ''
  ruleForm.introConfigId = ''
  dialogVisible.value = false
}
const open = (type, val) => {
  fromType.value = type
  if (type == 'update') {
    const { name, groupId, avator, ownerUserId, welcomeId, id, sceneId, introConfigId } = val
    assistantId.value = id
    ruleForm.name = name
    ruleForm.groupId = groupId
    ruleForm.sceneId = sceneId
    ruleForm.avator = avator
    ruleForm.ownerUserId = ownerUserId
    ruleForm.welcomeId = welcomeId
    ruleForm.introConfigId = introConfigId
  }
  dialogVisible.value = true
}
defineExpose({
  open
})
</script>

<style lang="scss" scoped>
.add-customer-dialog {
  :deep(.el-dialog) {
    border-radius: 16px;
    box-shadow: 0 20px 60px rgba(0, 82, 204, 0.15);
    border: 1px solid rgba(255, 255, 255, 0.2);
    backdrop-filter: blur(10px);
    background: rgba(255, 255, 255, 0.95);

    @media (max-width: 768px) {
      margin: 5vh auto;
      width: 95% !important;
      max-width: none !important;
    }
  }

  :deep(.el-dialog__header) {
    background: linear-gradient(135deg, #0052cc 0%, #003d99 100%);
    color: white;
    padding: 20px 24px;
    border-radius: 16px 16px 0 0;

    @media (max-width: 768px) {
      padding: 16px 20px;
    }

    .el-dialog__title {
      font-size: 18px;
      font-weight: 600;
      color: white;

      @media (max-width: 768px) {
        font-size: 16px;
      }
    }

    .el-dialog__headerbtn {
      top: 20px;
      right: 24px;

      @media (max-width: 768px) {
        top: 16px;
        right: 20px;
      }

      .el-dialog__close {
        color: white;
        font-size: 20px;

        &:hover {
          color: rgba(255, 255, 255, 0.8);
        }
      }
    }
  }

  :deep(.el-dialog__body) {
    padding: 0;
  }

  :deep(.el-dialog__footer) {
    padding: 0;
    border-top: 1px solid rgba(0, 82, 204, 0.1);
  }
}

.form-container {
  padding: 24px;

  @media (max-width: 768px) {
    padding: 20px 16px;
  }
}

.customer-form {
  .form-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 20px;

    @media (max-width: 768px) {
      grid-template-columns: 1fr;
      gap: 16px;
    }
  }

  .form-item {
    margin-bottom: 0;

    :deep(.el-form-item__label) {
      font-size: 14px;
      font-weight: 600;
      color: #172b4d;
      margin-bottom: 8px;
      line-height: 1.4;

      @media (max-width: 768px) {
        font-size: 13px;
        margin-bottom: 6px;
      }
    }

    :deep(.el-form-item__error) {
      font-size: 12px;
      color: #f56c6c;
      margin-top: 4px;
    }
  }

  .avatar-item {
    grid-column: 1 / -1;
    margin-top: 20px;

    @media (max-width: 768px) {
      margin-top: 16px;
    }
  }
}

.form-input {
  :deep(.el-input__wrapper) {
    border-radius: 10px;
    border: 2px solid #e4e7ed;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(5px);

    &:hover {
      border-color: rgba(0, 82, 204, 0.5);
      box-shadow: 0 2px 8px rgba(0, 82, 204, 0.1);
    }

    &.is-focus {
      border-color: #0052cc;
      box-shadow: 0 0 0 3px rgba(0, 82, 204, 0.1);
    }
  }

  :deep(.el-input__inner) {
    font-size: 14px;
    color: #606266;
    padding: 12px 16px;

    @media (max-width: 768px) {
      font-size: 13px;
      padding: 10px 14px;
    }

    &::placeholder {
      color: #c0c4cc;
      font-size: 13px;
    }
  }

  :deep(.el-input__suffix) {
    .el-input__clear {
      color: #c0c4cc;

      &:hover {
        color: #0052cc;
      }
    }
  }
}

.form-select {
  width: 100%;

  :deep(.el-select__wrapper) {
    border-radius: 10px;
    border: 2px solid #e4e7ed;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: rgba(255, 255, 255, 0.8);
    backdrop-filter: blur(5px);

    &:hover {
      border-color: rgba(0, 82, 204, 0.5);
      box-shadow: 0 2px 8px rgba(0, 82, 204, 0.1);
    }

    &.is-focused {
      border-color: #0052cc;
      box-shadow: 0 0 0 3px rgba(0, 82, 204, 0.1);
    }
  }

  :deep(.el-select__selected-item) {
    font-size: 14px;
    color: #606266;

    @media (max-width: 768px) {
      font-size: 13px;
    }
  }

  :deep(.el-select__placeholder) {
    color: #c0c4cc;
    font-size: 13px;
  }

  :deep(.el-select__caret) {
    color: #c0c4cc;

    &:hover {
      color: #0052cc;
    }
  }
}

.avatar-upload {
  display: flex;
  justify-content: flex-start;
  align-items: center;

  .upload-component {
    :deep(.el-upload) {
      border: 2px dashed rgba(0, 82, 204, 0.3);
      border-radius: 12px;
      background: rgba(0, 82, 204, 0.02);
      transition: all 0.3s ease;

      &:hover {
        border-color: #0052cc;
        background: rgba(0, 82, 204, 0.05);
      }
    }

    :deep(.el-upload-dragger) {
      border: none;
      background: transparent;

      &:hover {
        background: transparent;
      }
    }
  }
}

.dialog-footer {
  padding: 20px 24px;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
  display: flex;
  justify-content: center;

  @media (max-width: 768px) {
    padding: 16px 20px;
  }

  .submit-btn {
    background: linear-gradient(135deg, #0052cc 0%, #003d99 100%);
    border: none;
    border-radius: 10px;
    padding: 12px 32px;
    font-weight: 600;
    font-size: 14px;
    box-shadow: 0 4px 12px rgba(0, 82, 204, 0.3);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    width: 100%;
    max-width: 200px;

    @media (max-width: 768px) {
      width: 100%;
      max-width: none;
      padding: 14px;
    }

    &:hover:not(:disabled) {
      background: linear-gradient(135deg, #003d99 0%, #002966 100%);
      transform: translateY(-2px);
      box-shadow: 0 6px 16px rgba(0, 82, 204, 0.4);
    }

    &:active {
      transform: translateY(0);
    }

    &:disabled {
      background: #c0c4cc;
      box-shadow: none;
      transform: none;
    }

    .btn-icon {
      margin-right: 6px;
      font-size: 16px;
    }
  }
}

// 下拉选项样式
:deep(.el-select-dropdown) {
  border-radius: 10px;
  border: 1px solid rgba(0, 82, 204, 0.2);
  box-shadow: 0 8px 32px rgba(0, 82, 204, 0.1);

  .el-select-dropdown__item {
    font-size: 14px;
    padding: 10px 16px;
    transition: all 0.3s ease;

    &:hover {
      background: rgba(0, 82, 204, 0.1);
      color: #0052cc;
    }

    &.is-selected {
      background: linear-gradient(135deg, rgba(0, 82, 204, 0.1) 0%, rgba(0, 82, 204, 0.15) 100%);
      color: #0052cc;
      font-weight: 600;
    }
  }
}

// 加载动画优化
:deep(.el-loading-mask) {
  background-color: rgba(255, 255, 255, 0.8);
  backdrop-filter: blur(2px);

  .el-loading-spinner {
    .el-loading-text {
      color: #0052cc;
      font-weight: 500;
    }

    .circular {
      stroke: #0052cc;
    }
  }
}

// 动画效果
@keyframes slideInDown {
  from {
    opacity: 0;
    transform: translateY(-20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.form-container {
  animation: slideInDown 0.4s ease-out;
}

// 深色模式适配
@media (prefers-color-scheme: dark) {
  .add-customer-dialog {
    :deep(.el-dialog) {
      background: rgba(40, 40, 40, 0.95);
      border: 1px solid rgba(255, 255, 255, 0.1);
    }
  }

  .form-input,
  .form-select {
    :deep(.el-input__wrapper),
    :deep(.el-select__wrapper) {
      background: rgba(60, 60, 60, 0.8);
      border-color: rgba(255, 255, 255, 0.1);

      &:hover {
        border-color: rgba(0, 82, 204, 0.7);
      }
    }

    :deep(.el-input__inner),
    :deep(.el-select__selected-item) {
      color: #e0e0e0;

      &::placeholder {
        color: #909399;
      }
    }

    :deep(.el-select__placeholder) {
      color: #909399;
    }
  }

  .dialog-footer {
    background: linear-gradient(135deg, #2d2d2d 0%, #3a3a3a 100%);
  }

  .avatar-upload {
    .upload-component {
      :deep(.el-upload) {
        border-color: rgba(0, 82, 204, 0.5);
        background: rgba(0, 82, 204, 0.05);
      }
    }
  }
}

// 响应式优化
@media (max-width: 480px) {
  .add-customer-dialog {
    :deep(.el-dialog) {
      margin: 0 !important;
      width: 100% !important;
      height: 100vh !important;
      max-height: none !important;
      border-radius: 0 !important;
    }

    :deep(.el-dialog__header) {
      border-radius: 0 !important;
    }
  }

  .form-container {
    padding: 16px 12px;
  }

  .customer-form {
    .form-grid {
      gap: 12px;
    }
  }

  .dialog-footer {
    padding: 12px;

    .submit-btn {
      padding: 12px;
      font-size: 13px;
    }
  }
}
</style>
